package org.auth.service.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Created by aelns on 2017/3/11.
 */
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
	 @Override
	    public void addCorsMappings(CorsRegistry registry) {
	        registry.addMapping("/**")
	                .allowedOrigins("*")
	                .allowedHeaders("X-Requested-With", "Content-Type",  "X-Real-IP", "X-Forwarded-For", "Authorization", "appKey")
	                .allowCredentials(true).maxAge(3600);
	    }

	    /**
	     * 添加静态资源--过滤swagger-api (开源的在线API文档)
	     * @param registry
	     */
	    @Override
	    public void addResourceHandlers(ResourceHandlerRegistry registry) {
	        //过滤swagger
	        registry.addResourceHandler("swagger-ui.html")
	                .addResourceLocations("classpath:/META-INF/resources/");

	        registry.addResourceHandler("/webjars/**")
	                .addResourceLocations("classpath:/META-INF/resources/webjars/");

	        registry.addResourceHandler("/swagger-resources/**")
	                .addResourceLocations("classpath:/META-INF/resources/swagger-resources/");

	        registry.addResourceHandler("/swagger/**")
	                .addResourceLocations("classpath:/META-INF/resources/swagger*");

	        registry.addResourceHandler("/v2/api-docs/**")
	                .addResourceLocations("classpath:/META-INF/resources/v2/api-docs/");

	    }
	    
	    /**
	     * 添加自定义过滤器
	     */
	    
//	    @Override
//	    public void addInterceptors(InterceptorRegistry registry) {
//	        registry.addInterceptor(new ReqInterceptor()).addPathPatterns("/**");
//	    }
}
